﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CPPEI.Extensions
{
    public static class ValueConvertExtension
    {
        /// <summary>
        /// 将字符转换为boolean值，如果转换失败返回null
        /// </summary>
        /// <param name="valueString"></param>
        /// <returns></returns>
        public static bool? Convert(this string valueString)
        {
            bool result = false;
            if(bool.TryParse(valueString, out result))
            {
                return result;
            }
            return null;
        }

        /// <summary>
        /// 将字符串转换为boolean值，如果转换失败返回defaultValue
        /// </summary>
        /// <param name="valueString"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static bool Convert(this string valueString, bool defaultValue)
        {
            bool result = false;
            if (bool.TryParse(valueString, out result))
            {
                return result;
            }
            return defaultValue;
        }
    }
}
